#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../core/chat_hash.h"

/**
 * main
 */
void test_hash(int n)
{
	chat_hash_table_t *ht;
	int i;
	char *key;
	char *value;
	char *ret;

	ht = malloc(sizeof(chat_hash_table_t));
	memset(ht, 0, sizeof(chat_hash_table_t));
	
	chat_hash_init(ht, 1024);

	printf("run test_hash n=%d\n", n);
	for(i=0; i< n; i++){
		key = malloc(100);
		value = malloc(100);

		sprintf(key,"key_%d", i);
		sprintf(value,"value_%d", i);

		chat_hash_put(ht, key, value);
		free(key);
	}
	printf("put test finish\n");
	
	for(i=0; i< n; i++){
		key = malloc(100);
		value = malloc(100);

		sprintf(key,"key_%d", i);
		sprintf(value,"value_%d", i);

		ret = chat_hash_get(ht, key);

		if (strcmp(ret, value) != 0){
			printf("get key:%s value:%s getv:%s\n", key, value, ret);
			exit(EXIT_FAILURE);
		}

		free(key);
		free(value);
	}
	printf("get test success\n");

	for(i=0; i< n; i++){
		key = malloc(100);

		sprintf(key,"key_%d", i);

		ret = chat_hash_get(ht, key);
		free(ret);

		chat_hash_del(ht, key);

		ret = chat_hash_get(ht, key);
		if (ret != NULL){
			printf("del key:%s fail!! getv:%s\n", key, ret);
			exit(EXIT_FAILURE);
		}

		free(key);
	}
	printf("del test success\n");

	free(ht->items);
	free(ht);
}
